package com.example.siyun.controller.CouponsController;

import com.example.siyun.pojo.Coupons;
import com.example.siyun.pojo.UserList;
import com.example.siyun.service.couponsServer.CouponsServer;
import com.example.siyun.util.PageResult;
import com.example.siyun.util.SensitiveFilter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author 赵薇
 * @Date 2022/10/12 15:02
 * @PackageName:com.example.siyun.controller
 * @ClassName: CouponsController  优惠券控制层
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@Slf4j
public class CouponsController {

    @Autowired
    private CouponsServer couponsServer;

    /**
     * 限制敏感词实体类
     */
    @Autowired
    private SensitiveFilter sensitiveFilter;

    /**
     * 优惠券全部查询
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/Primary/getCoup")
    public PageResult getCoup(@RequestParam(defaultValue = "10") Integer page,
                              @RequestParam(defaultValue = "1") Integer limit,
                              String couponName,String couponManner,Integer couponRemainder){
        PageHelper.startPage(page,limit);

        List<Coupons> coupons = couponsServer.getCoup(couponName,couponManner,couponRemainder);
        PageInfo<Coupons> pageInfo = new PageInfo<>(coupons);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 添加优惠券
     * @param coupons
     * @return
     */
    @RequestMapping("/Primary/addCoup")
    public PageResult addCoup(Coupons coupons){
        log.info("添加优惠券：coupons:{}",coupons);
        String a1=coupons.getTime1();
        String a2=coupons.getTime2();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setLenient(false);
        try {
            Timestamp ts1 = new Timestamp(format.parse(a1).getTime());
            coupons.setStartTime(ts1);
            Timestamp ts2 = new Timestamp(format.parse(a2).getTime());
            coupons.setEndTime(ts2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //添加优惠券名称时不能使用敏感词，使用敏感词 替换为 *
        String filter = sensitiveFilter.filter(coupons.getCouponName());
        coupons.setCouponName(filter);

        couponsServer.addCoup(coupons);

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("添加成功!!");

        return result;
    }

    /**
     * 优惠券列表中 赠送优惠券
     * @param us
     * @return
     */
    @RequestMapping("/Primary/GiveCoup")
    public PageResult giveCoup(@RequestBody UserList[] us){
        log.info("赠送优惠券：us:{}",us);
        List<Integer> list = new ArrayList<>();
        Integer userId = null;
        for (int i = 0;i < us.length;i++){
            userId = us[i].getUserId();
            list.add(us[i].getCouponId());
        }
        couponsServer.giveCoup(userId,us.length,list);

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("赠送成功");
        return result;
    }

}
